﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Data;
using ThiagoSoftware.Dados;
using GerTestes.Entity;
using System.Data.SqlClient;

namespace GerTestes.Data
{
    public class DBSubModulos
    {
        #region Obter SubMódulos
        public DataTable getSubModulos(int pModulo)
        {
            string SQL = @"SELECT ts.ID_SUBMODULO,
                                   ts.ID_MODULO,
                                   ts.DESCRICAO,
                                   ts.ATIVO
                            FROM   TB_SUBMODULO ts
                            WHERE  ts.ID_MODULO = @ID_MODULO";
            DataAccess db = new DataAccess(SQL);
            db.AddParam("@ID_MODULO", SqlDbType.Int, pModulo);
            return db.GetDataTable();
        }
        #endregion

        #region Get Dados SubMódulo
        public SubModulo getDadosSubModulo(int pSubModulo)
        {
            string SQL = @"SELECT ts.ID_SUBMODULO,
                                   ts.ID_MODULO,
                                   ts.DESCRICAO,
                                   ts.ATIVO
                            FROM   TB_SUBMODULO ts
                            WHERE  ts.ID_SUBMODULO = @ID_SUBMODULO";
            DataAccess db = new DataAccess(SQL);
            db.AddParam("@ID_SUBMODULO", SqlDbType.Int, pSubModulo);
            try
            {
                db.AbrirConexao();
                SqlDataReader dr = db.ExecuteReader();
                SubModulo sub = new SubModulo();
                if (dr.Read())
                {
                    sub.CodModulo = Convert.ToInt32(dr["ID_MODULO"]);
                    sub.CodSubModulo = pSubModulo;
                    sub.Descricao = dr["DESCRICAO"].ToString();
                    sub.Ativo = dr["ATIVO"].ToString();                    
                }
                return sub;
            }
            catch
            {
                return null;
            }
        }
        #endregion

        #region Incluir SubMódulo
        public bool incluirSubModulo(SubModulo subModulo)
        {
            string SQL = @"INSERT INTO TB_SUBMODULO
                                  (
                                    ID_MODULO,
                                    DESCRICAO,
                                    ATIVO,
                                    DATA_CADASTRO
                                  )
                                VALUES
                                  (
                                    @ID_MODULO,
                                    @DESCRICAO,
                                    @ATIVO,
                                    GETDATE()
                                  )";
            DataAccess db = new DataAccess(SQL);
            db.AddParam("@ID_MODULO", SqlDbType.Int, subModulo.CodModulo);
            db.AddParam("@DESCRICAO", SqlDbType.VarChar, subModulo.Descricao);
            db.AddParam("@ATIVO", SqlDbType.VarChar, subModulo.Ativo);
            return db.ExecuteNonQuery();
        }
        #endregion

        #region Alterar SubMódulo
        public bool alterarSubModulo(SubModulo subModulo)
        {
            string SQL = @"UPDATE TB_SUBMODULO
                            SET    DESCRICAO      = @DESCRICAO,
                                   ATIVO          = @ATIVO
                            WHERE  ID_MODULO  = @ID_MODULO
                            AND ID_SUBMODULO = @ID_SUBMODULO";
            DataAccess db = new DataAccess(SQL);
            db.AddParam("@DESCRICAO", SqlDbType.VarChar, subModulo.Descricao);
            db.AddParam("@ATIVO", SqlDbType.VarChar, subModulo.Ativo);
            db.AddParam("@ID_MODULO", SqlDbType.Int, subModulo.CodModulo);
            db.AddParam("@ID_SUBMODULO", SqlDbType.Int, subModulo.CodSubModulo);
            return db.ExecuteNonQuery();
        }
        #endregion

        #region Excluir SubMódulo
        public bool excluirSubModulo(int pSubModulo)
        {
            string SQL = @"DELETE 
                            FROM   TB_SUBMODULO
                            WHERE  ID_SUBMODULO = @ID_SUBMODULO";
            DataAccess db = new DataAccess(SQL);
            db.AddParam("@ID_SUBMODULO", SqlDbType.Int, pSubModulo);
            return db.ExecuteNonQuery();
        }
        #endregion
    }
}
